<?php
class makehtml_listAction extends security_login {
	
	function action(){
		ModuleLimit(__CLASS__);
		set_time_limit(0);
		$miceSession = new session();
		$miceSession->set('makehtml','miceCMS');

		global $conn;
		$category=new model_Category();
		$category->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 ORDER BY path ASC');
		$results=$category->doSelect();
		$allid='';

		echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>';
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		echo '<link href="'.MICE_URL.MICE_TPL.'/admin/css/default.css" rel="stylesheet" type="text/css">';
		echo '</head><body>';
		echo '<table width="99%" align="center" border="0" cellpadding="0" cellspacing="0">  <tr align="center">    <td height="5"></td>  </tr></table>';
		echo '<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0">  <tr>		<td width="860" align="center" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#D5D5D5">      <tr>        <td height="28" bgcolor="#F9F9F9">&nbsp;<span class="STYLE7">更新分类列表HTML文件</span></td>      </tr>      <tr>        <td height="28" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0"><form id="form1" name="form1" method="post" action="">';
		
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;选择分类</td><td height="28" class="bottomline">';
		echo "<select name='path' id='path'><option value='all'>所有分类</option>";
		foreach($results as $crs){
			$checked='';
			if($_POST['path']==$crs->getCategoryId()){
				$checked='selected';
			}
			$allid=$allid.','.$crs->getCategoryId();
			echo "<option value='".$crs->getCategoryId()."' ".$checked.">".StrRepeat("─",$crs->getDepth()-1).$crs->getTitle()."</option>";
		}
		echo "</select>";
		echo '</td></tr>';
		
		$checked='';
		$checked1='';
		if($_POST['parent']==1){
			$checked='checked';
		}elseif($_POST['parent']==2){
			$checked1='checked';
		}
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;是否更新上级分类</td><td height="28" class="bottomline">';
		echo "<input name='parent' id='parent' type='radio' value='0' checked/>只更新此分类&nbsp;&nbsp;<input name='parent' id='parent' type='radio' value='1' ".$checked."/>更新此分类及此分类的所有上级分类&nbsp;&nbsp;<input name='parent' id='parent' type='radio' value='2' ".$checked1."/>更新此分类及此分类的所有下级分类";
		echo '</td></tr>';

		$checked2='';
		if($_POST['ishtml']){
			$checked2='checked';
		}

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;只更新未生成HTML的分类、<br>&nbsp;分类属性或分类文档做过改变的分类</td><td height="28" class="bottomline">';
		echo "<input name='ishtml' id='ishtml' type='radio' value='0' checked/>否&nbsp;&nbsp;<input name='ishtml' id='ishtml' type='radio' value='1' ".$checked2."/>是";
		echo '</td></tr>';

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;</td><td height="28" class="bottomline">';
		echo '</td></tr>';

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;</td><td height="28" class="bottomline">';
		echo '<input type="submit" name="Submit" value="开始生成HTML" /><br>(如果一次生成的分类过多,系统可能会较长时间没有反应,请耐心等待,不要做其它操作)';
		echo '</td></tr></form>';


		$miceSession = new session();
		if($_POST['path']){
			if($_POST['path']=='all'){
				$Cids=explode(',',$allid);
			}else{
				if($_POST['parent']==1){
					$category0=new model_Category();
					$category0->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND category_id='.$_POST['path']);
					$CategoryRS0=$category0->doSelectOne();
					$Cids=explode(',',$CategoryRS0->getPath());
				}elseif($_POST['parent']==2){
					$category0=new model_Category();
					$category0->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND path like \'%,'.$_POST['path'].',%\'');
					$CategoryRS0=$category0->doSelect();
					
					$searchpath=','.$_POST['path'].',';
					$paths=$searchpath;
					foreach($CategoryRS0 as $category){
						$pathlen=strpos($category->getPath(),$searchpath);
						$paths.=substr($category->getPath(),$pathlen+strlen($searchpath));
					}
					$Cids=explode(',',$paths);
				}else{
					$Cids[0]=$_POST['path'];
				}
			}
			
			$categoryname='';
			$ishtmlsql='';
			if($_POST['ishtml']){
				$ishtmlsql=' AND ishtml=0';
			}
			foreach($Cids as $CategoryId){
				if(is_numeric($CategoryId)){
					$category=new model_Category();
					$category->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0'.$ishtmlsql.' AND category_id='.$CategoryId.' ORDER BY path');
					$CategoryRS=$category->doSelectOne();

					if($CategoryRS){
						$categoryname.=$CategoryRS->getTitle();
						$turn='';
						if($CategoryRS->getUrl()){
							//跳转
							$turn='<html><body><form action="'.MiceRestoreQuote('micefun',$CategoryRS->getUrl()).'"  method="post" id="link" name="link"  target="_blank"  style="display:none;"></form><Script LANGUAGE="JavaScript">document.getElementById("link").submit();history.back(-1);</script></body></html>';
						}
						if($CategoryRS->getTpl()){								//模板
							$tpl=$CategoryRS->getTpl();
						}else{
							$tpl='index/list.php';
						}
					$miceSession = new session();
					$limit='';
					if($miceSession->get('mu__grouplevel')<$CategoryRS->getGroupId() AND $CategoryRS->getGroupId()>0){
						$limit='<body><iframe src="'.MICE_URL.'index.php/limit/0/'.$CategoryRS->getGroupId().'/0"  style="width:0px; height:0px;" frameborder="0"></iframe>';
					}else{
						$limit='<body><iframe src="'.MICE_URL.'index.php/limit/0/0/0"  style="width:0px; height:0px;" frameborder="0"></iframe>';
					}

					$list_page=1;
					for($PageId=1;$PageId<=$list_page;$PageId++){
						$content='';
						$smarty = new Smarty();
						$smarty->load_filter('pre','micePreCompileIndex');
						$smarty->assign('CategoryRS',$CategoryRS);
						$smarty->assign('CategoryId',$CategoryId);
						$smarty->assign('PageId',$PageId);

						//将内容写入至一个静态文件
						if($turn==''){
							$content = $smarty->fetch($tpl); 
							if($limit!=''){
								$content = str_replace("<body>", $limit, $content);
							}
						}else{
							$content = $turn;
						}
						$htmlfn=GetHtmlPathList($CategoryRS->getCategoryId(),$CategoryRS->getFilename(),$CategoryRS->GetListrule(),$PageId);
						if(!$htmlfn){
							echo '<tr><td height="28" class="bottomline" width="300">&nbsp;生成结果</td><td height="28" class="bottomline"><font color="red">生成错误:请检查html目录是否有写入权限</font><br></td></tr></form>';
							echo '</table></td></tr></table></td></tr></table><br></body></html>';
							die();
						}
						$fp = fopen(MICE_DIR.$htmlfn, 'w'); 
						fwrite($fp, $content); 
						fclose($fp);

						if($PageId==1){
							$categoryname.='&nbsp;&nbsp;&nbsp;<a href="'.MICE_URL.$htmlfn.'" target="_blank">浏览此分类……</a><br>';
						}

						//获取分页数
						$list_page=$miceSession->get('list_page');
					}
					//对已生成html的栏目做标记
					if(!$CategoryRS->getIshtml()){
						$category->setIshtml(1);
						$category->doUpdate();
					}
				}
			  }
			}
		}
		if(!$categoryname){
			$categoryname='本次操作没有更新任何分类';
		}
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;生成结果</td><td height="28" class="bottomline"><br>';
		echo $categoryname;
		echo '<br></td></tr></form>';
		echo '</table></td></tr></table></td></tr></table><br></body></html>';
		$miceSession->set('makehtml','');
	}
}
?>